/*
Name:comment_data
Author: Terry Dong
Description: data access layer
*/
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
	public partial class comment_data:Icomment_data
	{
		private DbSqlServer helper;
		
		#region constructors
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public comment_data()
        {
			helper = new DbSqlServer(ConnectionString.SqlServer_GPriceCollect);
        }
		
		/// <summary>
        /// default constructor with the defaultConn
        /// </summary>
		public comment_data(string connString)
		{
			helper = new DbSqlServer(connString);	
		}
		#endregion
		
		#region IDataBase<comment_data> 

        public int Add(comment_dataEntity entity)
        {
           	StringBuilder strSql=new StringBuilder();
			strSql.Append("INSERT INTO comment_data(");
			strSql.Append("point,title,detail,author,create_time,data_id)");
			strSql.Append(" VALUES (");
			strSql.Append("@point,@title,@detail,@author,@create_time,@data_id)");
			SqlParameter[] parameters = {
					new SqlParameter("@point",SqlDbType.Int),
					new SqlParameter("@title",SqlDbType.VarChar),
					new SqlParameter("@detail",SqlDbType.Text),
					new SqlParameter("@author",SqlDbType.VarChar),
					new SqlParameter("@create_time",SqlDbType.BigInt),
					new SqlParameter("@data_id",SqlDbType.UniqueIdentifier)
					};
				parameters[0].Value = entity.point;
				parameters[1].Value = entity.title;
				parameters[2].Value = entity.detail;
				parameters[3].Value = entity.author;
				parameters[4].Value = entity.create_time;
				parameters[5].Value = entity.data_id;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
        public int Update(comment_dataEntity entity)
        {
			
           StringBuilder strSql=new StringBuilder();
			strSql.Append("UPDATE comment_data SET ");
				strSql.Append("point=@point,");
				strSql.Append("title=@title,");
				strSql.Append("detail=@detail,");
				strSql.Append("author=@author,");
				strSql.Append("create_time=@create_time,");
				strSql.Append("data_id=@data_id");
		
			strSql.Append(" WHERE comment_id=@comment_id");
			SqlParameter[] parameters = {
					new SqlParameter("@comment_id",SqlDbType.UniqueIdentifier),
					new SqlParameter("@point",SqlDbType.Int),
					new SqlParameter("@title",SqlDbType.VarChar),
					new SqlParameter("@detail",SqlDbType.Text),
					new SqlParameter("@author",SqlDbType.VarChar),
					new SqlParameter("@create_time",SqlDbType.BigInt),
					new SqlParameter("@data_id",SqlDbType.UniqueIdentifier)
					};
				parameters[0].Value = entity.comment_id;
				parameters[1].Value = entity.point;
				parameters[2].Value = entity.title;
				parameters[3].Value = entity.detail;
				parameters[4].Value = entity.author;
				parameters[5].Value = entity.create_time;
				parameters[6].Value = entity.data_id;
				
			return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
		
		public int UpdateBycomment_id(comment_dataEntity entity)
        { 
            string where=string.Format("comment_id='{0}'",entity.comment_id);
			entity.comment_id=null;
        	return Updatepartial(entity, where);
		}

        public int Delete(string strWhere)
        {
           StringBuilder strSql=new StringBuilder();
			strSql.Append("DELETE FROM comment_data ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteNonQuery(strSql.ToString());
        }

        public comment_dataEntity GetEntity(int primaryKeyId)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select * from comment_data ");
			strSql.Append(" where comment_id=@primaryKeyId");
			SqlParameter[] parameters = {
					new SqlParameter("@primaryKeyId", SqlDbType.Int)};
			parameters[0].Value = primaryKeyId;
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString(), parameters) as SqlDataReader;
            if (dr.HasRows)
            {
                dr.Read();
				comment_dataEntity entity=new comment_dataEntity();
				if(!Convert.IsDBNull(dr["comment_id"]))
                {
                    entity.comment_id = (Guid)dr["comment_id"];
                }
				if(!Convert.IsDBNull(dr["point"]))
                {
                    entity.point = (int)dr["point"];
                }
				entity.title=dr["title"].ToString();		
				entity.detail=dr["detail"].ToString();		
				entity.author=dr["author"].ToString();		
				if(!Convert.IsDBNull(dr["create_time"]))
                {
                    entity.create_time = (long)dr["create_time"];
                }
				if(!Convert.IsDBNull(dr["data_id"]))
                {
                    entity.data_id = (Guid)dr["data_id"];
                }
				dr.Close();
            	dr.Dispose();
				return entity;
			}
			else
			{
				dr.Close();
            	dr.Dispose();
				return null;
			}
        }

        public object GetCell(string cellName, string where)
        {
            string strSql = string.Empty;
            if (string.IsNullOrEmpty(where))
            {
                strSql = string.Format("SELECT {0} FROM comment_data", cellName);
            }
            else
            {
                strSql = string.Format("SELECT {0} FROM comment_data Where {1}", cellName, where);
            }
            return helper.ExecuteCell(strSql);
        }

		public DataTable GetTable(string strWhere)
		{
           StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT comment_id,point,title,detail,author,create_time,data_id");
			strSql.Append(" FROM comment_data ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" WHERE "+strWhere);
			}
			return helper.ExecuteAdapter(strSql.ToString());
		}
		
        public SortList<comment_dataEntity> GetList(string strWhere)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select comment_id,point,title,detail,author,create_time,data_id");
			strSql.Append(" FROM comment_data ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
			SortList<comment_dataEntity> list= new SortList<comment_dataEntity>();
			while (dr.Read())
            {
				comment_dataEntity entity=new comment_dataEntity();
				if(!Convert.IsDBNull(dr["comment_id"]))
                {
                    entity.comment_id = (Guid)dr["comment_id"];
                }
				if(!Convert.IsDBNull(dr["point"]))
                {
                    entity.point = (int)dr["point"];
                }
				entity.title=dr["title"].ToString();
				entity.detail=dr["detail"].ToString();
				entity.author=dr["author"].ToString();
				if(!Convert.IsDBNull(dr["create_time"]))
                {
                    entity.create_time = (long)dr["create_time"];
                }
				if(!Convert.IsDBNull(dr["data_id"]))
                {
                    entity.data_id = (Guid)dr["data_id"];
                }
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }
		
				
		#region select,add,update the fields that you want to operate.
		
		public int AddPartial(comment_dataEntity entity)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildAdd(entity, "comment_data", ref sql);

            if (param!=null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
           return reval;            
            
        }
		
		public int Updatepartial(comment_dataEntity entity, string where)
		{
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildUpdate(entity, "comment_data",where, ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql,param);
            }
            return reval;     
		}
		
		public List<comment_dataEntity> GetPartial(string[] selectedFields,string where)
        {
            List<comment_dataEntity> list = new List<comment_dataEntity>();
            string sql = DataUtility.BuildSelect(selectedFields, "comment_data", where);
            if (!string.IsNullOrEmpty(sql))
            {
                DbDataReader reader= helper.ExecuteReader(sql);
             
                while (reader.Read())
                {
                    comment_dataEntity model = new comment_dataEntity();
                    foreach (string field in selectedFields)
                    {
                        PropertyInfo propInfo = model.GetType().GetProperty(field);
                        propInfo.SetValue(model, reader[field], null);
                    }

                    list.Add(model);
                }

                reader.Close();
            }
            return list;
        }
		#endregion
		

        #endregion
	}
}
